#include<iostream>
#include<signal.h>
#include<unistd.h>
#include<wait.h>
#include<string>
#include<sys/types.h>
#include<sys/wait.h>
#include<functional>
#include<vector>

using namespace std;

typedef function<void()>func;
vector<func> callbacks;

uint64_t count =0;

//实验1:利用alarm实现定时器功能(待测试 )

void show_count()
{
    cout<<"final count :"<<count <<endl;
}

void show_log()
{
    cout<<"这是输出日志功能"<<endl;
}


void catchsig(int signum)
{
    cout<<"final count:"<<count<<endl;
    alarm(1);
}

void log_user()
{
    if(fork()==0)
    {
        execl("/usr/bin/who","who",nullptr);
        exit(1);
    }
    wait(nullptr);
}

static void Usage(string proc)
{
    cout<<"usage:\r\n\t"<<proc<<"signumber processid"<<endl;
}

void catch_sig(int signum)
{
    for(auto&m:callbacks)
    {
        m();
    }
    //设定每间隔一秒钟，执行callbacks中的全部任务
    alarm(1);
}

int main(int argc , char*argv[])
{
    signal(SIGSEGV,catch_sig);
    alarm(1);

    callbacks.push_back(show_count);
    callbacks.push_back(show_log);
    while(true) count++;
}
